pkgname=ltp
pkgver=git
pkgrel=1
url='https://github.com/linux-test-project/ltp'
arch=('i386' 'x86_64')
license=('GPL')
options=('!strip')
source=('https://git.centos.org/r/rpms/rusers.git' 'https://github.com/PeterHuewe/tpm-emulator.git' 'https://github.com/numactl/numactl.git' 'https://github.com/linux-test-project/ltp.git')
md5sums=('SKIP' 'SKIP' 'SKIP' 'SKIP')

. $LKP_SRC/lib/tests/ltp.sh

build_rusers()
{
	echo build_rusers

	cd $srcdir/rusers || return

	benchmark_path=${pkgdir}/lkp/benchmarks/${pkgname}

	local branch=$(git branch -a | grep /c | tail -1 | awk -F/ '{print $NF}')
	git checkout "$branch" || return

	local rusers_version=$(grep ^Version: SPECS/rusers.spec | awk '{print $2}')

	local source=$(grep ^Source: SPECS/rusers.spec | awk '{print $2}')

	# below url was from http://sourceforge.net/projects/rstatd/
	# in SPECS/rusers.spec

	wget --no-check-certificate -c "https://sourceforge.net/projects/rstatd/files/rstatd/4.0.1/rpc.rstatd-4.0.1.tar.gz/download" -O - | tar -xz || exit 1

	for p in $(grep ^Patch SPECS/rusers.spec | awk '{print $2}')
	do
		echo "Patching $p ..."
		patch -f -p0 < SOURCES/$p
	done

	# rpc*/ currently means rpc.rstatd-4.0.1/
	cd rpc*/ || return
	./configure --prefix="${benchmark_path}"/netkit-rusers

	make
}

build_tpm_emulator()
{
	echo build_tpm_emulator

	cd $srcdir/tpm-emulator || return

	# the lastest tpm-emulator is currupted so use old one
	git reset --hard 0f4579e
	# remove Werror to use gcc-7
	sed -i "s/-Werror//g" CMakeLists.txt || return
	# don't build kernel module
	sed -i "s/add_subdirectory(tpmd_dev)/#add_subdirectory(tpmd_dev)/g" CMakeLists.txt || return

	mkdir build || return

	cd build
	cmake .. || return
	make || return
	make install || return
	make package
}

build_numactl()
{
	echo build_numactl

	cd $srcdir/numactl || return

	benchmark_path=${pkgdir}/lkp/benchmarks/${pkgname}

	autoreconf -f -i
	./configure --prefix="${benchmark_path}"/testcases/bin/numactl
	make
}

install_rusers()
{
	echo install_rusers

	cd $srcdir/rusers || return

	mkdir -p "${benchmark_path}"/netkit-rusers/{bin,man/man1}

	local rusers_version=$(grep ^Version: SPECS/rusers.spec | awk '{print $2}')
	cd rpc*/
	make install 2> /dev/null
}

install_tpm_emulator()
{
	echo install_tpm_emulator

	cd $srcdir/tpm-emulator || return

	benchmark_path=${pkgdir}/lkp/benchmarks/${pkgname}

	mkdir -p "${benchmark_path}"/tpm-emulater
	cp build/*.zip "${benchmark_path}"/tpm-emulater
}

install_numactl()
{
	echo install_numactl

	cd $srcdir/numactl || return

	benchmark_path=${pkgdir}/lkp/benchmarks/${pkgname}

	make install
	ln -sf "${benchmark_path}"/testcases/bin/numactl/bin/numademo "${benchmark_path}"/testcases/bin/numademo
	ln -sf "${benchmark_path}"/testcases/bin/numactl/bin/memhog "${benchmark_path}"/testcases/bin/memhog
}

build()
{
	split_syscalls_tests

	cd $srcdir/$pkgname || return

	echo build_ltp
	build_ltp "${pkgdir}/lkp/benchmarks/${pkgname}" || return

	# build_tpm_emulator || return
	build_numactl || return

	[ "$DISTRO" = "debian" -o "$DISTRO" = "ubuntu" ] && {
		build_rusers || return
	}

	return 0
}

hide()
{
	mv $1 .$1
}

split_syscalls_tests()
{
	echo split_syscalls_tests

	cd $srcdir/$pkgname || return

	local timer_tests
	timer_tests=$(grep -lr "sample = sample_fn" ./) || return

	cd $srcdir/$pkgname/runtest || return

	for timer_test in $timer_tests; do
		timer_test=${timer_test##*/}
		timer_test=${timer_test%%.*}
		grep "^$timer_test" syscalls >> syscalls-timer
	done

	grep "^tgkill" syscalls >> syscalls-tgkill || return
	grep "^setsockopt06" syscalls >> syscalls-setsockopt06 || return
	# need 2 numa node
	grep -Ee "^(mbind|move_pages|migrate_pages|set_mempolicy)" syscalls >> syscalls-numa || return

	# standalone
	standalone_file=$LKP_SRC/pkg/${pkgname}/addon/tests/standalone
	while read line
	do
		echo $line > ./"${line//[[:blank:]]/.}"
	done < <(cat $standalone_file | grep -v '^#')

	grep -v -f syscalls-timer -f syscalls-tgkill -f syscalls-setsockopt06 -f syscalls-numa syscalls -f $standalone_file > syscalls-rest || return
	$LKP_SRC/tools/split-tests syscalls-rest 8 syscalls-
	hide syscalls-rest
	hide syscalls
}

split_tests()
{
	echo split_tests

	benchmark_path="${pkgdir}/lkp/benchmarks/${pkgname}"
	cd $benchmark_path/runtest || return

	$LKP_SRC/tools/split-tests dio 4 dio-
	hide dio

	$LKP_SRC/tools/split-tests fs_readonly 4 fs_readonly-
	hide fs_readonly

	$LKP_SRC/tools/split-tests fs 4 fs-
	hide fs

	$LKP_SRC/tools/split-tests crashme 2 crashme-
	hide crashme

	$LKP_SRC/tools/split-tests scsi_debug.part1 4 scsi_debug.part1-
	hide scsi_debug.part1

	$LKP_SRC/tools/split-tests ltp-aiodio.part2 2 ltp-aiodio.part2-
	hide ltp-aiodio.part2

	$LKP_SRC/tools/split-tests ltp-aiodio.part4 2 ltp-aiodio.part4-
	hide ltp-aiodio.part4

	# net_stress.appl
	grep "ssh" net_stress.appl > net_stress.appl-ssh || return
	grep "dns" net_stress.appl > net_stress.appl-dns || return
	grep "http" net_stress.appl > net_stress.appl-http || return
	grep "ftp" net_stress.appl > net_stress.appl-ftp || return
	grep -v -e ^# -e ^$ -e ssh -e dns -e http -e ftp net_stress.appl && return 1
	hide net_stress.appl

	# syscalls-ipc
	grep "msgstress" syscalls-ipc > syscalls-ipc-msgstress || return
	grep -v "msgstress" syscalls-ipc > syscalls-ipc-00 || return
	hide syscalls-ipc

	# mm
	grep -e oom mm > mm-oom || return
	grep -e min_free_kbytes mm > mm-min_free_kbytes || return
	grep -v -e oom -e min_free_kbytes mm > mm-00 || return
	hide mm
}

package()
{
	cd $srcdir/$pkgname
	install_ltp "${pkgdir}/lkp/benchmarks/${pkgname}" || return

	# install_tpm_emulator || return
	install_numactl || return

	[ "$DISTRO" = "debian" -o "$DISTRO" = "ubuntu" ] && {
		install_rusers || return
	}

	split_tests || return

	return 0
}
